package com.training.admin.visitor.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.JsonUtils;
import com.training.admin.entity.category.TrainingCategory;
import com.training.admin.entity.course.Course;
import com.training.admin.entity.department.Department;
import com.training.admin.entity.project.ProjectPhase;
import com.training.admin.entity.project.ProjectPhaseCourse;
import com.training.admin.entity.trainee.IdmUser;
import com.training.admin.entity.trainee.Trainee;
import com.training.admin.service.CourseService;
import com.training.admin.service.DepartmentService;
import com.training.admin.service.IdmUserService;
import com.training.admin.service.ProjectPhaseCourseService;
import com.training.admin.service.TraineeService;
import com.training.admin.service.TrainingCategoryService;

/**
 * 
 * @author Rockett
 *
 */
@Component("persistVisitor")
public class PersistVisitor {
    
    private static final Logger logger = LoggerFactory.getLogger(PersistVisitor.class);
	
	@Autowired
	private GenericDaoSupport genericDaoSupport;
	
	@Autowired
	private TrainingCategoryService trainingCategoryService;
	
	@Autowired
	private DepartmentService departmentService;
	
	@Autowired
	private TraineeService traineeService;
	
	@Autowired
	private CourseService courseService;
	
	@Autowired
	private ProjectPhaseCourseService projectPhaseCourseService;
	
	@Autowired
	private IdmUserService idmUserService;
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	public Department department(Long id) {
		if(id==null){
			return new Department();
		}
			
		return genericDaoSupport.load(Department.class, id);
	}

	/**
	 * 
	 * @return
	 */
	public List<Department> departments() {
		return genericDaoSupport.loadAll(Department.class);
	}
	
	/**
	 * @return
	 */
    public String getSubCategory() {
	    
	    Map<String, List<Map<String, String>>> result = new HashMap<String,List<Map<String, String>>>();
	    
	    List<TrainingCategory> trainingCategories = trainingCategoryService.listSecondCategory();
	    
	    Map<String, List<TrainingCategory>> tMap = CollectionUtils.constructMapValues(trainingCategories, "parentId");
	    
	    for(String key : tMap.keySet()) {
	        
	        List<TrainingCategory> item = tMap.get(key);
	        
	        List<Map<String, String>> itemList = new ArrayList<>();
	        
	        for(TrainingCategory trainingCategory : item) {
	            
	            Map<String, String> map = new HashMap<String,String>();
	            
	            map.put("id", trainingCategory.getId().toString());
	            
	            map.put("text", trainingCategory.getName());
	            
	            itemList.add(map);
	            
	        }
	        result.put(key, itemList);
	                
	    }
	    return JsonUtils.toJson(result);
	}
	
	/**
	 * @return
	 */
	public List<TrainingCategory> getTrainingCategories(){
	    
	    List<TrainingCategory> trainingCategories = trainingCategoryService.listFirstCategory();
	    
	    return trainingCategories;
	    
	}
	
	/**
	 * @return
	 */
	public List<Department> getDeparments(){
	    
	    List<Department> departments = departmentService.loadAll(Department.class);
	    
	    return departments;
	    
	}
	
	/**
	 * @return
	 */
	public List<Trainee> getTrainees(){
	    
	    List<Trainee> trainees = traineeService.loadAll(Trainee.class);
	    
	    return trainees;
	    
	}
	
	/**
	 * @param departmentId
	 * @return
	 */
	public Department organize(Long departmentId) {
	    return departmentService.load(Department.class, departmentId);
	}
	
	/**
	 * @param code
	 * @return
	 */
	public Trainee trainee(String code) {
	    return traineeService.loadByCode(code);
	}
	
	/**
	 * @return
	 */
	public List<Course> getCourses(){
	    logger.debug("#getCourse() - get course from database");
        
        List<Course> courses = courseService.listAll(Course.class);
        
        return courses;
	}
	
	/**
	 * @param projectPhase
	 * @return
	 */
	public List<ProjectPhaseCourse> projectPhaseCourses(ProjectPhase projectPhase) {
	    List<ProjectPhaseCourse> projectPhaseCourses = projectPhaseCourseService.listByProjectPhase(projectPhase);
        return projectPhaseCourses;
	}

	/**
	 * @param projectLead
	 * @return
	 */
	public IdmUser idmUser(String projectLead) {
	    return idmUserService.getIdmUser(projectLead);
	}
	
	public IdmUser idmUserByEmail(String email) {
        return idmUserService.getIdmUserByEmail(email);
    }
}
